package com.wardrobe.soa.backend.banner;

import com.google.common.collect.Maps;
import com.wardrobe.domain.banner.BannerBean;
import com.wardrobe.domain.banner.BannerShowBean;
import com.wardrobe.domain.common.BackEndFromBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/15.
 */
@RestController
@RequestMapping("/backend/banner")
public class BackBannerController {
    private static final Logger logger = LoggerFactory.getLogger(BackBannerController.class);
    @Autowired
    private BackBannerService backBannerService;

    @RequestMapping("/bannerList")
    public Map<String, Object> bannerList() {
        Map<String, Object> result = Maps.newHashMap();
        List<BannerShowBean> list = backBannerService.getBannerList(-2, 0);
        list.stream().forEach(bannerShowBean -> {
            bannerShowBean.setCreateTime(bannerShowBean.getCreateTime().substring(0,bannerShowBean.getCreateTime().length()-2));
            bannerShowBean.setUpdateTime(bannerShowBean.getUpdateTime().substring(0,bannerShowBean.getUpdateTime().length()-2));
        });
        result.put("list", list);
        return result;
    }

    @RequestMapping("/addBanner")
    public Map<String,Object> addBanner(BannerBean bannerBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            int status = backBannerService.addBanner(bannerBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","添加成功");
            }else{
                result.put("status",2);
                result.put("msg","添加失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","添加失败");
            logger.error("addBanner error",e);
        }
        return result;
    }

    @RequestMapping("/deleteBannerById")
    public int deleteBannerById(int id){
        return backBannerService.deleteBanner(id);
    }
    @RequestMapping("/modifyBanner")
    public Map<String,Object> modifyBanner(BackEndFromBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status = backBannerService.modifyBanner(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","修改成功");
            }else{
                result.put("status",2);
                result.put("msg","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","修改失败");
            logger.error("modifyBanner error",e);
        }
        return result;
    }

}
